import {
  list
} from '../../../../api/ad'
import {
  getInfo
} from '../../../../api/mch'
const util = require('../../../../utils/util')
let app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // indicatorDots: true, //是否显示面板指示点
    autoplay: false, //是否自动播放
    interval: 6000, //停留时间间隔
    duration: 400, //播放时长
    previousMargin: '130rpx', //前边距，可用于露出前一项的一小部分，接受 px 和 rpx 值
    nextMargin: '130rpx', //后边距，可用于露出前一项的一小部分，接受 px 和 rpx 值
    circular: true, //是否采用衔接滑动
    current: 1, //swiper当前索引
    tagIds: [],
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    photoList: [],
    color: [],
    c: '',
    pageNum: 1,
    pageSize: 20,
    mch: { logo: '', name: '', shortName: '' },
  },
  lifetimes: {
    attached() {
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
      this.setData({
        tagIds: this.data.tagIds
      })
      if (this.data.info.rows > 3) this.data.pageSize = this.data.info.rows;
      this.loadAd()
    }
  },
  created() {
  },
  /**
   * 组件的方法列表
   */
  methods: {
    loadAd() {
      var _this = this
      list({
        mchId: app.globalData.mchId,
        tagIds: this.data.tagIds,
        pageSize:this.data.pageSize
      }).then(res => {
        res.data.data.forEach(element => {
          element.backgroundColor = "linear-gradient(180deg," + element.backgroundColor + ", rgba(248, 248, 248, 1))"
          _this.data.color.push(element.backgroundColor)
          element.thumbnail = util.thumbnail(element.thumbnail, 350, (5 / 4) * 350)
          this.data.c = this.data.color[0]
        });
        _this.data.photoList = res.data.data
        if (res.data.data.length < 3) {
          this.setData({
            previousMargin: '370rpx',
            nextMargin: '0',
          })
        }
        this.setData({
          color: this.data.color,
          photoList: _this.data.photoList,
          c: this.data.c,
          statusBarHeight: app.globalData.statusBarHeight,
        });
      });
    },
    bintapClick() {
      this.navTo(this.data.photoList[this.data.current])
    },
    navTo(val) {
      if (val.hasOwnProperty('typeId') && !util.isNull(val.typeId)) {
        if (val.type === 0) {
          wx.navigateTo({
            url: '/pages/goods/product/details?id=' + val.typeId
          })
        } else if (val.type === 9) {
          wx.navigateTo({
            url: '/pages/index/wxPage/wxPage?id=' + val.typeId
          })
        } else if (val.type === 2) {
          wx.navigateTo({
            url: '/pages/member/detail/detail?id=' + val.typeId,
          })
        } else if (val.type === 1) {
          wx.navigateTo({
            url: '/pages/goods/category/category?id=' + val.typeId
          })
        }
      }
    },
    toLink(e) {
      const { type } = e.currentTarget.dataset
      if (type.hasOwnProperty('typeId') && !util.isNull(type.typeId)) {
        if (type.type === 0) {
          wx.navigateTo({
            url: '/pages/goods/product/details?id=' + type.typeId
          })
        } else if (type.type === 9) {
          wx.navigateTo({
            url: '/pages/index/wxPage/wxPage?id=' + type.typeId
          })
        } else if (type.type === 2) {
          wx.navigateTo({
            url: '/pages/member/detail/detail?id=' + type.typeId,
          })
        } else if (type.type === 1) {
          wx.navigateTo({
            url: '/pages/goods/category/category?id=' + type.typeId
          })
        }
      }
    },
    swiperBindchange(e) {
      const { current } = e.detail
      this.setData({
        current: current,
      })
    },

    swiperClick(e) {
      const { index } = e.currentTarget.dataset
      if (index == this.data.current) {
        this.bintapClick()
      }
      this.setData({
        current: index
      })
    },
  }
})